<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateManagerAPIRequest;
use App\Http\Requests\API\UpdateManagerAPIRequest;
use App\Models\Manager;
use App\Repositories\ManagerRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;
use Illuminate\Support\Facades\Auth;


/**
 * Class ManagerController
 * @package App\Http\Controllers\API
 */

class ManagerAPIController extends AppBaseController
{
    /** @var  ManagerRepository */
    private $managerRepository;

    public function __construct(ManagerRepository $managerRepo)
    {
        $this->managerRepository = $managerRepo;
    }

    /**
     * Display a listing of the Manager.
     * GET|HEAD /managers
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->managerRepository->pushCriteria(new RequestCriteria($request));
        $this->managerRepository->pushCriteria(new LimitOffsetCriteria($request));
        $managers = $this->managerRepository->paginate($request->limit);

        return $this->sendResponse($managers->toArray(), '获取成功');
    }

    /**
     * Store a newly created Manager in storage.
     * POST /managers
     *
     * @param CreateManagerAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateManagerAPIRequest $request)
    {
        $input = $request->all();
        $user = Auth::user();
        $user = obj2arr($user);
        $input['password'] = md5(md5("parkingwang.com").$request->password);
        $input['cid'] = 38;
        $input['create_by'] = $user['id'];
        $input['update_by'] = $user['id'];
        $manager = $this->managerRepository->findWhere(["username"=>$input['username']]);
        if (!empty($manager->id)) {
            return $this->sendError('用户名已经存在');
        }
        $managers = Manager::insert($input);

        return $this->sendResponse($managers, '操作成功');
    }

    /**
     * Display the specified Manager.
     * GET|HEAD /managers/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var Manager $manager */
        $manager = $this->managerRepository->findWithoutFail($id);

        if (empty($manager)) {
            return $this->sendError('数据不存在');
        }

        return $this->sendResponse($manager->toArray(), '获取成功');
    }

    /**
     * Update the specified Manager in storage.
     * PUT/PATCH /managers/{id}
     *
     * @param  int $id
     * @param UpdateManagerAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateManagerAPIRequest $request)
    {
        $input = $request->all();
        $user = Auth::user();
        $user = obj2arr($user);
        $input['update_by'] = $user['id'];
        unset($input['password']);
        /** @var Manager $manager */
        $manager = $this->managerRepository->findWithoutFail($id);

        if (empty($manager)) {
            return $this->sendError('数据不存在');
        }

        $manager = $this->managerRepository->update($input, $id);

        return $this->sendResponse($manager->toArray(), '操作成功');
    }

    /**
     * Remove the specified User from storage.
     * PUT /users/change-password
     * @param  Request $request
     *
     * @return mixed
     */
    public function changePwd(Request $request)
    {
        $this->validate($request, ['password' => 'required|max:32', 'new_password' => 'required|max:32','id'=>'int']);
        $input = $request->all();
        if(empty($input['id'])){
            return $this->sendError('缺少用户id');
        }
        if(empty($input['new_password'])){
            return $this->sendError('缺少重置密码');
        }
        $user = $this->managerRepository->findWithoutFail($input['id']);
        if ($request->password != $request->new_password) {
            return $this->sendError('密码输入不一致');
        }

        $user->password = md5(md5("parkingwang.com").$request->new_password); //$request->new_password;
        $user->save();
        return $this->sendResponse([], '密码修改成功');
    }

    /**
     * Remove the specified Manager from storage.
     * DELETE /managers/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var Manager $manager */
        $manager = $this->managerRepository->findWithoutFail($id);

        if (empty($manager)) {
            return $this->sendError('数据不存在');
        }

        $manager->delete();

        return $this->sendResponse($id, '操作成功');
    }
}
